<template>
  <!--  最外层大盒子，用于装左侧主题展示区和右侧的登录界面区-->
  <div class="main">
    <!--左侧主题展示区-->
    <div class="background">
      <img src="../assets/images/login-background.jpg" alt="登录界面左边图片">
      <div class="background-title">
        <div class="background-title-box">
          <span class="background-title-theme">智能守护 健康同行</span>
          <span class="background-title-introduce">专业级健康监护  24小时贴心守护</span>
        </div>
        <div class="background-introduce-box">
          <div class="background-introduce1">
            <i class="el-icon-monitor"></i>
            <span class="theme">实时监控</span>
            <span class="introduce">持续追踪心率、血氧、睡眠等健康指标</span>
          </div>
          <div class="background-introduce2">
            <i class="el-icon-data-analysis"></i>
            <span class="theme">数据分析</span>
            <span class="introduce">智能算法分析，提供个性化健康建议</span>
          </div>
        </div>

      </div>
    </div>
<!--    右侧登录界面区-->
    <div class="login">
      <div class="login-form-title">
        <img src="../assets/logo/logo.png" alt="logo">
        <h3 class="title">健康穿戴管理系统</h3>
      </div>
      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
        <el-form-item prop="username" class="account">
          <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="请输入账号或邮箱">
            <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <el-form-item prop="password" class="secret">
          <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码" @keyup.enter.native="handleLogin">
            <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
          </el-input>
        </el-form-item>
        <div class="code" v-if="captchaEnabled">
          <el-form-item prop="code"  class="code-input">
            <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" @keyup.enter.native="handleLogin">
              <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
            </el-input>
          </el-form-item>
          <img :src="codeUrl" @click="getCode" class="login-code-img"  alt="验证码"/>
        </div>
        <div class="remember_forget">
          <el-checkbox v-model="loginForm.rememberMe" class="rf-remember">记住我</el-checkbox>
          <span class="rf-forget"  @click="drawer_secret = true">忘记密码？</span>
        </div>
        <el-drawer
          class="drawer"
          title="密码重置"
          :visible.sync="drawer_secret"
          direction="rtl"
          :before-close="handleClose">
          <span>维护中。。。</span>
        </el-drawer>

        <el-form-item>
          <el-button :loading="loading" size="medium" type="primary" @click.native.prevent="handleLogin" class="login-button">
            <span v-if="!loading">登 录</span>
            <span v-else>登 录 中...</span>
          </el-button>
          <div class="other">
            <div class="other-login">
              <div class="line"></div>
              <span>其他登录方式</span>
              <div class="line"></div>
            </div>
            <div class="other-login-icon">
              <img src="../assets/images/wechat-icon.svg" alt="微信" class="wechat-icon">
              <img src="../assets/images/qq-icon.svg" alt="QQ" class="qq-icon">
            </div>
          </div>
          <div  v-if="register" class="register-login">
            <span>还没有账号？</span>
            <router-link class="link-type" :to="'/register'">立即注册</router-link>
          </div>

        </el-form-item>
      </el-form>
      <!--  底部  -->
      <div class="el-login-footer">
        <span>Copyright © 2018-2024 kangyue.vip All Rights Reserved.</span>
      </div>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login";
import Cookies from "js-cookie";
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      drawer_secret: false,
      loginRules: {
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: false,
      // 注册开关
      register: true,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img;
          this.loginForm.uuid = res.uuid;
        }
      });
    },
    getCookie() {
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{});
          }).catch(() => {
            this.loading = false;
            if (this.captchaEnabled) {
              this.getCode();
            }
          });
        }
      });
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {

        });
    }
  }
};
</script>

<style>
.main{
  display:flex;
  width: 100%;
  height: 100%;
  border-radius: 10px;
}
/*——————————主题界面分割线—————————————————————————————————————--————————————————————————————-*/
.background{
  position: relative;
  width: 60%;
  height: 100%;
  display: flex;
}
.background img{
  position: absolute;
  width: 100%;
  height: 100%;
  z-index: 0;

}
.background .background-title{
  position: absolute;
  width: 100%;
  height: 100%;
  background: linear-gradient(to left, rgba(66, 144, 222, 0.66), rgb(66, 74, 222));
  z-index: 1;
  display: flex;
  //justify-content: center;
  flex-direction: column;

}
.background-title-box {
  display: flex;
  flex-direction: column;
  margin-top: 20%;
}
.background-title-theme{
  font-size: 40px;
  font-weight: bold;
  color: white;
  margin-left: 60px;
  margin-bottom: 15px;
}
.background-title-introduce{
  font-size: 12px;
  margin-left: 60px;
  color: white;
  margin-bottom: 15px;
}
.background-introduce-box{
  display: flex;
  margin-top: 5%;

}
.background-introduce1{
  display: flex;
  flex-direction: column;
  margin-left: 60px;
  background-color: rgba(255, 255, 255, 0.1);
  padding: 10px;
  border-radius: 10px;
}
.background-introduce2{
  display: flex;
  flex-direction: column;
  margin-left: 20px;
  background-color: rgba(255, 255, 255, 0.1);
  padding: 10px;
  border-radius: 10px;
}
.el-icon-monitor{
  color: white;
  margin: 5px;
}
.el-icon-data-analysis{
  color: white;
  margin: 5px;
}
.theme{
  font-size: 12px;
  font-weight: bold;
  color: white;
  margin: 5px;
}
.introduce{
  font-size: 10px;
  color: white;
  margin: 5px;
}
/*———————————登录界面分割线———————————————————————————————————————————————————————————————————————————————————————————*/
.login{
  width:40%;
  height: 100%;
  background-color: #ffffff;
  display: flex;
  flex-direction: column;
}
.login-form-title{
  display: flex;
  height: 20%;
  width: 100%;
  align-items: center;
}
.login-form-title img{
  width: 40px;
  height: 40px;
  margin-left: 10%;
  margin-right: 10px;
}
.login-form-title h3{

  font-size: 30px;
  font-weight: bold;
}
.login-form{
   height: 70%;
   width: 100%;
   display: flex;
   flex-direction: column;

 }
.account{
  margin-top: 15%;
  margin-left: 20%;
  width: 60%;
}
.secret{
  margin-left: 20%;
  width: 60%;
}
.code{
  display: flex;
}
.code .code-input{
  margin-left: 20%;
  width: 40%;
}
.code img{
  margin-left: 5px;
  width: 19%;
  height: 62%;
  border-radius: 3px;
}
.remember_forget{
  display: flex;
  text-align: center;
}
.rf-remember{
  margin-left: 20%;
}
.rf-forget{
  font-size: 15px;
  margin-left:37%;
  margin-top: -1.5px;
}
.rf-forget:hover{
  user-select: none;
  cursor: pointer;
  color: #00afff;
}
.login-button{
  margin-top: 3%;
  margin-left: 20%;
  width: 60%;
}
.other{
  display: flex;
  flex-direction: column;
  margin-top: 3%;
  align-items: center;
}
.other-login{
  display: flex;
}
.other-login-icon img{
  width: 30px;
  height: 30px;
}
.wechat-icon{
  margin-right: 5px;
  border: none;
  cursor: pointer;
  border-radius: 30%;
  transition: box-shadow 0.5s ease-in-out ;
}
.wechat-icon:hover{

  box-shadow:
    0px 0px 20px rgba(0, 0, 0, 0.7);
}
.qq-icon{
  margin-left: 5px;
  border: none;
  cursor: pointer;
  border-radius: 30%;
  transition: box-shadow 0.5s ease-in-out ;
}
.qq-icon:hover{
  box-shadow:
    0px 0px 20px rgba(0, 0, 0, 0.7);
}
.register-login{
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: white;
}
.el-login-footer{
   display: flex;
   height: 10%;
   justify-content: center;
   align-items: center;
 }
/*___________动画分割线_________________________________________________________________________________*/

</style>
